home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / src / pl-dll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-18  |  1.8 KB  |  113 lines

  1. /*  $Id: pl-dll.c,v 1.5 1998/02/18 13:56:46 jan Exp $
  2.  
  3.     Part of SWI-Prolog.
  4.  
  5.     Purpose: Windows DDL interface
  6. */
  7.  
  8. #if defined(__WINDOWS__) || defined(__WIN32__)
  9.  
  10. #include "windows.h"
  11. #include "pl-incl.h"
  12. #include <stdio.h>
  13.  
  14. #ifdef O_DLL
  15.  
  16. extern char *WinError(void);
  17.  
  18. #define MAX_DLL_INSTANCES    32    /* handle allocation */
  19.  
  20. static HINSTANCE dll[MAX_DLL_INSTANCES];
  21.  
  22. static int
  23. allocDllHandle(HINSTANCE handle)
  24. { int i;
  25.  
  26.   for(i=0; i<MAX_DLL_INSTANCES; i++)
  27.   { if ( !dll[i] )
  28.     { dll[i] = handle;
  29.       return i;
  30.     }
  31.   }
  32.  
  33.   warning("DLL manager: out of handles");
  34.   return -1;
  35. }
  36.  
  37.  
  38. static int
  39. get_dll_handle(term_t handle, int *hdl)
  40. { int i;
  41.  
  42.   if ( PL_get_integer(handle, &i) &&
  43.        i >= 0 && i < MAX_DLL_INSTANCES && dll[i] )
  44.   { *hdl = i;
  45.  
  46.     succeed;
  47.   }
  48.  
  49.   fail;
  50. }
  51.  
  52.  
  53. static word
  54. dll_warning(char *fmt)
  55. { return warning("%s failed: %s", fmt, WinError());
  56. }
  57.  
  58.  
  59.  
  60. word
  61. pl_open_dll(term_t name, term_t handle)
  62. { HINSTANCE h;
  63.   char *s;
  64.  
  65.   if ( !PL_get_chars(name, &s, CVT_ALL) )
  66.     return warning("open_dll/2: illegal name");
  67.  
  68.   if ( (h = LoadLibrary(s)) )
  69.   { int plhandle = allocDllHandle(h);
  70.  
  71.     return PL_unify_integer(handle, plhandle);
  72.   }
  73.  
  74.   return dll_warning("open_dll/2");
  75. }
  76.  
  77.  
  78. word
  79. pl_close_dll(term_t handle)
  80. { int i;
  81.  
  82.   if ( !get_dll_handle(handle, &i) )
  83.     return warning("close_dll/1: illegal handle");
  84.   
  85.   FreeLibrary(dll[i]);
  86.   dll[i] = NULL;
  87.  
  88.   succeed;
  89. }
  90.  
  91.  
  92. word
  93. pl_call_dll_function(term_t handle, term_t funcname)
  94. { int i;
  95.   FARPROC proc;
  96.   char *fname;
  97.  
  98.   if ( !get_dll_handle(handle, &i) )
  99.     return warning("call_dll_function/2: illegal handle");
  100.   if ( !PL_get_chars(funcname, &fname, CVT_ALL) )
  101.     return warning("call_dll_function/2: illegal function name");
  102.   
  103.   if ( !(proc = GetProcAddress(dll[i], fname)) )
  104.     fail;
  105.  
  106.   (*proc)();
  107.  
  108.   succeed;
  109. }
  110.  
  111. #endif /*O_DLL*/
  112. #endif /*defined(__WINDOWS__) || defined(__WIN32__)*/
  113.